class PurchaseOrder < ActiveRecord::Base
  belongs_to :supplier
  has_many :po_payments
  
  #====== validations =========
  validates_presence_of :supplier_id, :amount
  
  
  
  # 初始化采购合同的已付金额和余额
  def init_amount_paid_balance
    self.amount_paid = 0
    self.balance = self.amount
  end
  
  # 增加已付金额 
  def increase_amount_paid(amount) 
    logger.info("amount input is : #{amount}" )
    logger.info("amount_paid is : #{self.amount_paid}" )
    self.amount_paid = self.amount_paid + amount
  end
  
  # 减少已付金额 
  def decrease_amount_paid(amount) 
    logger.info("amount input is : #{amount}" )
    logger.info("amount_paid is : #{self.amount_paid}" )
    self.amount_paid = self.amount_paid - amount
  end
  
  # 跟新余额
  def cal_balance 
    self.balance = self.amount - self.amount_paid
  end
  
end
